#!/usr/bin/env Rscript

library(tidyverse)
library(lubridate)
library(padr)

print("Loading complete data file...")
complete <- read_csv2("../app/data/completo.csv", na = "NA",
                      col_types = cols(
                          pais = col_character(),
                          regiao = col_character(),
                          uf = col_character(),
                          mesorregiao = col_character(),
                          microrregiao = col_character(),
                          municipio = col_character(),
                          st_acidente_feriado = col_character(),
                          ds_agente_causador = col_character(),
                          ano_cat = col_integer(),
                          ds_cnae_classe_cat = col_character(),
                          dt_acidente = col_date(),
                          st_dia_semana_acidente = col_character(),
                          ds_emitente_cat = col_character(),
                          hora_acidente = col_time(),
                          idade_cat = col_integer(),
                          cd_indica_obito = col_character(),
                          ds_natureza_lesao = col_character(),
                          ds_cbo = col_character(),
                          ds_parte_corpo_atingida = col_character(),
                          cd_tipo_sexo_empregado_cat = col_character(),
                          ds_tipo_acidente = col_character(),
                          ds_tipo_local_acidente = col_character()
                      ))

#Estimation of population for each year by "municipio"
print("Loading estimation files...")
est2012  <- read_csv2("../app/data/estimativa_municipio_2012.csv",
                      na = "NA",
                      col_types = cols(
                          uf = col_character(),
                          municipio = col_character(),
                          populacao = col_integer(),
                          ano = col_integer()
                      ))
est2013  <- read_csv2("../app/data/estimativa_municipio_2013.csv",
                      na = "NA",
                      col_types = cols(
                          uf = col_character(),
                          municipio = col_character(),
                          populacao = col_integer(),
                          ano = col_integer()
                      ))
est2014  <- read_csv2("../app/data/estimativa_municipio_2014.csv",
                      na = "NA",
                      col_types = cols(
                          uf = col_character(),
                          municipio = col_character(),
                          populacao = col_integer(),
                          ano = col_integer()
                      ))
est2015  <- read_csv2("../app/data/estimativa_municipio_2015.csv",
                      na = "NA",
                      col_types = cols(
                          uf = col_character(),
                          municipio = col_character(),
                          populacao = col_integer(),
                          ano = col_integer()
                      ))
est2016  <- read_csv2("../app/data/estimativa_municipio_2016.csv",
                      na = "NA",
                      col_types = cols(
                          uf = col_character(),
                          municipio = col_character(),
                          populacao = col_integer(),
                          ano = col_integer()
                      ))
est2017  <- read_csv2("../app/data/estimativa_municipio_2017.csv",
                      na = "NA",
                      col_types = cols(
                          uf = col_character(),
                          municipio = col_character(),
                          populacao = col_integer(),
                          ano = col_integer()
                      ))

#Summarise population
est12 <- est2012 %>% group_by(uf, municipio) %>% summarise(populacao = sum(populacao))
est13 <- est2013 %>% group_by(uf, municipio) %>% summarise(populacao = sum(populacao))
est14 <- est2014 %>% group_by(uf, municipio) %>% summarise(populacao = sum(populacao))
est15 <- est2015 %>% group_by(uf, municipio) %>% summarise(populacao = sum(populacao))
est16 <- est2016 %>% group_by(uf, municipio) %>% summarise(populacao = sum(populacao))
est17 <- est2017 %>% group_by(uf, municipio) %>% summarise(populacao = sum(populacao))

#summarise accidents
print("Summarising accidents...")
acd12 <- complete %>%
    filter(ano_cat == 2012) %>%
    group_by(uf, microrregiao, municipio) %>%
    summarise(acidentes = n())

acd13 <- complete %>%
    filter(ano_cat == 2013) %>%
    group_by(uf, microrregiao, municipio) %>%
    summarise(acidentes = n())

acd14 <- complete %>%
    filter(ano_cat == 2014) %>%
    group_by(uf, microrregiao, municipio) %>%
    summarise(acidentes = n())

acd15 <- complete %>%
    filter(ano_cat == 2015) %>%
    group_by(uf, microrregiao, municipio) %>%
    summarise(acidentes = n())

acd16 <- complete %>%
    filter(ano_cat == 2016) %>%
    group_by(uf, microrregiao, municipio) %>%
    summarise(acidentes = n())

acd17 <- complete %>%
    filter(ano_cat == 2017) %>%
    group_by(uf, microrregiao, municipio) %>%
    summarise(acidentes = n())

#Join estimations tables and c
print("Joining tables...")
percent_est12 <- inner_join(acd12, est12, by = c("uf", "municipio")) %>%
    select(uf, microrregiao, acidentes, populacao) %>%
    group_by(microrregiao) %>%
    summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
    mutate(percentage = (acidentes/populacao) * 100)

percent_est13 <- inner_join(acd13, est13, by = c("uf", "municipio")) %>%
    select(uf, microrregiao, acidentes, populacao) %>%
    group_by(microrregiao) %>%
    summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
    mutate(percentage = (acidentes/populacao) * 100)

percent_est14 <- inner_join(acd14, est14, by = c("uf", "municipio")) %>%
    select(uf, microrregiao, acidentes, populacao) %>%
    group_by(microrregiao) %>%
    summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
    mutate(percentage = (acidentes/populacao) * 100)

percent_est15 <- inner_join(acd15, est15, by = c("uf", "municipio")) %>%
    select(uf, microrregiao, acidentes, populacao) %>%
    group_by(microrregiao) %>%
    summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
    mutate(percentage = (acidentes/populacao) * 100)

percent_est16 <- inner_join(acd16, est16, by = c("uf", "municipio")) %>%
    select(uf, microrregiao, acidentes, populacao) %>%
    group_by(microrregiao) %>%
    summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
    mutate(percentage = (acidentes/populacao) * 100)

percent_est17 <- inner_join(acd17, est17, by = c("uf", "municipio")) %>%
    select(uf, microrregiao, acidentes, populacao) %>%
    group_by(microrregiao) %>%
    summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
    mutate(percentage = (acidentes/populacao) * 100)

print("Writing csv files...")
write_delim(percent_est12, "../app/data/percentage_micro2012.csv", delim = ";")
write_delim(percent_est13, "../app/data/percentage_micro2013.csv", delim = ";")
write_delim(percent_est14, "../app/data/percentage_micro2014.csv", delim = ";")
write_delim(percent_est15, "../app/data/percentage_micro2015.csv", delim = ";")
write_delim(percent_est16, "../app/data/percentage_micro2016.csv", delim = ";")
write_delim(percent_est17, "../app/data/percentage_micro2017.csv", delim = ";")

print("Writing rds file...")
saveRDS(percent_est12, "../app/data/rds/percentage_micro2012.rds")
saveRDS(percent_est13, "../app/data/rds/percentage_micro2013.rds")
saveRDS(percent_est14, "../app/data/rds/percentage_micro2014.rds")
saveRDS(percent_est15, "../app/data/rds/percentage_micro2015.rds")
saveRDS(percent_est16, "../app/data/rds/percentage_micro2016.rds")
saveRDS(percent_est17, "../app/data/rds/percentage_micro2017.rds")

print("Finishing gracefully!")
